'''
Created on 2011-9-20

@author: Administrator
'''
import resultparse.base
from xml.etree.ElementTree import ElementTree

import logging

def XunitResultParse(xml):
    resultlist = []
    try:
        ET = ElementTree()
        el =  ET.parse(xml)
        if el.tag != 'testsuites':
            return 
        for v in el.getchildren():
            if v.tag != 'testsuite':
                continue
            for cv in v.getchildren():
                if cv.tag != 'testcase':
                    continue
                ci  = resultparse.base.CaseInfo()
                ci.name = v.attrib['name'] + '.' + cv.attrib['name']
                ci.runtime = cv.attrib['time']
                failure =   cv.find('failure')
                if failure != None:
                    ci.result = resultparse.base.FAILURE
                    ci.message = failure.attrib['message']
                else:
                    ci.result = resultparse.base.PASS
                resultlist.append(ci)
    except Exception as e:
        logging.exception(e)
    return resultlist
